package world.liquor.controller;

import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import world.liquor.domain.BeerAssessment;
import world.liquor.domain.BeerInfo;
import world.liquor.service.BeerAssessmentService;
import world.liquor.service.myBeerService;
import world.member.domain.Member;


@Controller
public class myBeerController {
	
	private myBeerService myBeerService;	
	private BeerAssessmentService service2;
	public myBeerController() {
	}
	@Autowired
	public myBeerController(myBeerService myBeerService, BeerAssessmentService service2) {
		this.myBeerService = myBeerService;
		this.service2=service2;
	}
	
	@RequestMapping("/liquor/beerWantEat.do")
	public String beerWantEat(ModelMap map, HttpSession session) throws SQLException{
		Member member=(Member)session.getAttribute("login_member");
		String memberId=member.getMemberId();
		ArrayList<BeerInfo>list=myBeerService.selectBeerByIdAssessment(memberId);
		ArrayList<BeerAssessment> list_as = common(session);
		if(list.size()==0){
			map.addAttribute("result", list);		
			map.addAttribute("message", "관심있는 맥주가 없습니다.");
			return "/member/mypage.jsp";
		}
		map.addAttribute("result", list);		
		map.addAttribute("result2", list_as);
		map.addAttribute("message", "마실래요");
		return "/member/mypage.jsp";
	}
	
	@RequestMapping("/liquor/myAssessment.do")
	public String myAssessment(ModelMap map, HttpSession session) throws SQLException{
		Member member=(Member)session.getAttribute("login_member");
		String memberId=member.getMemberId();
		ArrayList<BeerInfo>list=myBeerService.selectBeerByIdNotAssessment(memberId);
		ArrayList<BeerAssessment> list_as = common(session);
		if(list.size()==0){
			map.addAttribute("result", list);		
			map.addAttribute("message", "평가한 맥주가 없습니다.");
			return "/member/mypage.jsp";
		}
		map.addAttribute("result", list);	
		map.addAttribute("result2", list_as);
		map.addAttribute("message", "내가 평가한 맥주");
		return "/member/mypage.jsp";
	}
	
	//공통 메서드 - 회원별로 평가한 assessment 점수목록 list_as를 보내주기 위한 메서드
		public ArrayList<BeerAssessment> common(HttpSession session) throws SQLException{
			Member m = (Member)session.getAttribute("login_member");
			if(m!=null){ //session값이 있을 경우에만 list_as 리턴
				String memberId = m.getMemberId();
				ArrayList<BeerAssessment> list_as = service2.selectAssessmentByMemberId(memberId);
				return list_as;			
			}
			return null;
		}


	

}
